﻿// version 20101211
// Author: cike.org
// --------------------------------------------------------------------
// ==UserScript==
// @name          百度贴吧地带
// @namespace     http://tieba.baidu.com/
// @description  	 for baidu tieba
// @include       	http://tieba.baidu.com/*
// @include      	http://xs.tieba.baidu.com/*
// @exclude		http://tieba.baidu.com/i/*
// ==/UserScript==

function setCookie(name,value)
{
	var exp  = new Date();
	exp.setMonth(exp.getMonth() + 24);
	document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString()+";domain=tieba.baidu.com;path=/";
}
function getCookie(name)
{
	if (document.cookie.length>0){
		start=document.cookie.indexOf(name + "=")
		if (start!=-1){ 
			start=start + name.length+1 
			end=document.cookie.indexOf(";",start)
			if (end==-1) 
				end=document.cookie.length
			return unescape(document.cookie.substring(start,end))
		} 
	}
	return "";
}
function loadfile(fname,id){
	var _fname;
	_fname = fname.indexOf("?")!=-1 ? fname.substring(0,fname.indexOf("?")) : fname;
	var ftype = _fname.substring(_fname.lastIndexOf(".")+1);
	switch(ftype){
		case "js":
			var e=document.createElement("script");
  			e.src=fname;
  			break;
  		case "css":
  			 var e=document.createElement("link") ;
			 e.rel = "stylesheet";
			 e.type="text/css";
			 e.href=fname;
			 break;
		default:return;
	}
	if(id)e.id=id;
	header.appendChild(e) 
}

function autoPage(e){
	var  LEFT = 37;
	var  RIGHT = 39;

	if(e.ctrlKey || e.shiftKey || e.altKey || e.metaKey || isText(e.target)) 
		return ;
	else if(e.keyCode == LEFT || e.keyCode == RIGHT){
		getPageNode(e.keyCode);
	}
	
	function isText(node){
		var name = node.localName.toLowerCase();
		if (name == 'textarea' || name == 'input' || name == 'select' || (name == 'div' && node.id.toLowerCase().indexOf('textarea')!=-1))
			return true;
		else
			return false;
	}
	
	function goPage(node){
		if(node.onclick)
			node.onclick();
		if(node.click)
			node.click();
		if(node.href)
			location.href = node.href;
	}
	
	function getPageNode(keyCode){
		var strs;
		if(keyCode == LEFT)
			strs = '上一页';
		else if(keyCode == RIGHT)
			strs = '下一页';
		var pnode = document.getElementById("thread_footer") || document.getElementById("pagebar");
		var nodes = unsafeWindow.document.evaluate("//a[(text()='"+strs+"')]", pnode, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
		if(nodes.snapshotLength > 0) 
			goPage(nodes.snapshotItem(0));
	}

}


var config = {
	version:	"20101211",
	host:"http://sqia.cn/tieba-zone/"
	//host:"http://test.cn/cike.org/tieba-skin/"
	//host:"http://tiebazone.appspot.com/"
}
var header = document.getElementsByTagName("head")[0];
var js = header.getElementsByTagName("script");
var hasJq = false;
for(var i=0,j = js.length;i<j;i++){
	if(js[i].src.indexOf("jquery") != -1) hasJq = true;
}

loadfile(config['host']+"css/skin.css","Tsk-css");
loadfile(config['host']+"js/skin.js?v="+config.version,"Tsk-js");

if(!getCookie("Tsk_Open")) setCookie("Tsk_Open",1);

if(getCookie("Tsk_Open")==1){
	if(!hasJq) 
		loadfile("http://static.tieba.baidu.com/tb/js/lib/jquery-1.4.2.js");
	
	
	if(document.getElementById("thread_footer") || document.getElementById("pagebar"))
		unsafeWindow.document.addEventListener('keydown', autoPage, false); //翻页参考了hotkey next page
	document.getElementsByTagName("html")[0].className="Tsk";
}





